using System;
using System.Collections.Generic;
using System.Linq;
#if WINDOWS
using System.IO;
#endif
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using System.Xml;
using System.IO.IsolatedStorage;
using System.IO;
#endif
namespace GameFirst
{
    public class menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D sfondomenu;
        Texture2D bottone1;
        Texture2D bottone1s;
        Texture2D bottone2;
        Texture2D bottone2s;
        Texture2D bottone3;
        Texture2D bottone3s;
        Texture2D bottone4;
        Texture2D bottone4s;
        #if WINDOWS
        Texture2D mirino;
        #endif
        SoundEffect song;
        SoundEffectInstance songI;
        bool b1;
        bool b2;
        bool b3;
        bool b4;
        SpriteBatch spriteBatch;
        public menu(Game game)
            : base(game)
        {
            //game.IsMouseVisible = true;
            b1 = false;
            b2 = false;
            b3 = false;
            b4 = false;
            menucomp.SaveExist = false;
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            #if WINDOWS
            mirino = Game.Content.Load<Texture2D>("mirino");
            #endif 
            sfondomenu = Game.Content.Load<Texture2D>("titolo con sfondo");
            bottone1 = Game.Content.Load<Texture2D>("newgame");
            bottone1s = Game.Content.Load<Texture2D>("newgames");
            bottone2 = Game.Content.Load<Texture2D>("options");
            bottone2s = Game.Content.Load<Texture2D>("optionss");
            bottone3 = Game.Content.Load<Texture2D>("exit");
            bottone3s = Game.Content.Load<Texture2D>("exits");
            bottone4 = Game.Content.Load<Texture2D>("loadgame");
            bottone4s = Game.Content.Load<Texture2D>("loadgames");
 
            song = Game.Content.Load<SoundEffect>("menu");
            songI = song.CreateInstance();
            songI.IsLooped = true;
            
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            //AUDIO MENù
            songI.Volume = menucomp.SongVol;
            if (songI.State != SoundState.Playing)
                songI.Play();

            b1 = false;
            b2 = false;
            b3 = false;
            b4 = false;
            if ((InputGame.InputManagerGame.GetState().mouseX < ((int)(GraphicsDevice.Viewport.Width / 8.5f) + (int)(GraphicsDevice.Viewport.Width / 3.5f))) && (InputGame.InputManagerGame.GetState().mouseX > ((int)(GraphicsDevice.Viewport.Width / 8.5f))))
            {
                //bottone1 new game
                if ((InputGame.InputManagerGame.GetState().mouseY < ((int)(GraphicsDevice.Viewport.Height / 1.80f) + GraphicsDevice.Viewport.Height / 8)) && (InputGame.InputManagerGame.GetState().mouseY > (int)( GraphicsDevice.Viewport.Height / 1.80f)))
                {
                    b1 = true;
                    
                    if (InputGame.InputManagerGame.GetState().click)
                    {
                        songI.Stop();
                        menucomp.SaveExist = false;
                        while (Game.Components.Count > 0)
                        {
                            ((GameComponent)Game.Components[0]).Dispose();
                        }
                        Game.Components.Clear();
                        AirSimulation.Azzera();
                        Game.Components.Add(new GameLogic(Game));
                        Game.Components.Add(new Gamerendering(Game));
                        Game.Components.Add(new InputGame.InputManagerGame(Game));
                    }
                }
                //bottone 4 load game
                if ((InputGame.InputManagerGame.GetState().mouseY < (GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 4.5f) + GraphicsDevice.Viewport.Height / 2)) && (InputGame.InputManagerGame.GetState().mouseY > (GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 4.5f))))
                {
                    b4 = true;
                    
                    if (InputGame.InputManagerGame.GetState().click)
                    {
                        songI.Stop();

                        #if WINDOWS
                        if (File.Exists("savegame.sav"))
                            menucomp.SaveExist = true;
                        #elif WINDOWS_PHONE
                        IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                        if(myIsolatedStorage.FileExists("savegame.xml"))
                            menucomp.SaveExist = true;
                        #endif

                        while (Game.Components.Count > 0)
                        {
                            ((GameComponent)Game.Components[0]).Dispose();
                        }
                        Game.Components.Clear();

                        Game.Components.Add(new GameLogic(Game));
                        Game.Components.Add(new Gamerendering(Game));
                        Game.Components.Add(new InputGame.InputManagerGame(Game));
                    }
                }
            }
            if ((InputGame.InputManagerGame.GetState().mouseX < ((int)(GraphicsDevice.Viewport.Width / 1.7f) + (int)(GraphicsDevice.Viewport.Width / 3.5f))) && (InputGame.InputManagerGame.GetState().mouseX > ((int)(GraphicsDevice.Viewport.Width / 1.7f))))
            {
                //bottone2 option
                if ((InputGame.InputManagerGame.GetState().mouseY < ((int)(GraphicsDevice.Viewport.Height / 1.80f) + GraphicsDevice.Viewport.Height / 8)) && (InputGame.InputManagerGame.GetState().mouseY > (int)(GraphicsDevice.Viewport.Height / 1.80f)))
                {
                    b2 = true;
                    
                    if (InputGame.InputManagerGame.GetState().click)
                    {
                        songI.Stop();
                        while (Game.Components.Count > 0)
                        {
                            ((GameComponent)Game.Components[0]).Dispose();
                        }
                        Game.Components.Clear();
                        Game.Components.Add(new Opzioni(Game));
                        Game.Components.Add(new InputGame.InputManagerGame(Game));

                    }
                }
                //bottone3 exit
                if ((InputGame.InputManagerGame.GetState().mouseY < (GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 4.5f) + GraphicsDevice.Viewport.Height / 2)) && (InputGame.InputManagerGame.GetState().mouseY > (GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 4.5f))))
                {
                    b3 = true;
                    if (InputGame.InputManagerGame.GetState().click)
                    {
                        songI.Stop();
                        Game.Exit();
                    }
                }
                
            }
            #if WINDOWS
            if (Keyboard.GetState().IsKeyDown(Keys.Enter))
            {
                songI.Stop();
                while (Game.Components.Count > 0)
                {
                    ((GameComponent)Game.Components[0]).Dispose();
                }
                Game.Components.Clear();

                Game.Components.Add(new GameLogic(Game));
                Game.Components.Add(new Gamerendering(Game));
                Game.Components.Add(new InputGame.InputManagerGame(Game));
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                songI.Stop();
                Game.Exit();
            }
            #endif
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Violet);
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            //spriteBatch.Begin(BlendState.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, SpriteScale);
            //spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicWrap, null, null);
            //spriteBatch.Draw(sfondomenu, Vector2.Zero, new Rectangle(0, 0, 2218, 1774), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
            #if WINDOWS
            spriteBatch.Draw(sfondomenu, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 2218, 1774), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
            #elif WINDOWS_PHONE
            spriteBatch.Draw(sfondomenu, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 800, 480), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
#endif
            //controllo se passo sopra con il mouse al bottone corrispondente
            if (b1 == true)
                spriteBatch.Draw(bottone1s, new Rectangle((int)(GraphicsDevice.Viewport.Width / 8.5f),(int)( GraphicsDevice.Viewport.Height / 1.80f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            else
                spriteBatch.Draw(bottone1, new Rectangle((int)(GraphicsDevice.Viewport.Width / 8.5f), (int)(GraphicsDevice.Viewport.Height / 1.80f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            
            if (b2 == true)
                spriteBatch.Draw(bottone2s, new Rectangle((int)(GraphicsDevice.Viewport.Width / 1.7f), (int)(GraphicsDevice.Viewport.Height / 1.80f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            else
                spriteBatch.Draw(bottone2, new Rectangle((int)(GraphicsDevice.Viewport.Width / 1.7f), (int)(GraphicsDevice.Viewport.Height / 1.80f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);

            if (b3 == true)
                spriteBatch.Draw(bottone3s, new Rectangle((int)(GraphicsDevice.Viewport.Width / 1.7f), GraphicsDevice.Viewport.Height / 2 +(int)( GraphicsDevice.Viewport.Height / 4.5f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            else
                spriteBatch.Draw(bottone3, new Rectangle((int)(GraphicsDevice.Viewport.Width / 1.7f), GraphicsDevice.Viewport.Height / 2 + (int) (GraphicsDevice.Viewport.Height / 4.5f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            //load
            if (b4 == true)
                spriteBatch.Draw(bottone4s, new Rectangle((int)(GraphicsDevice.Viewport.Width / 8.5f), GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 4.5f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            else
                spriteBatch.Draw(bottone4, new Rectangle((int)(GraphicsDevice.Viewport.Width / 8.5f), GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 4.5f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            #if WINDOWS
            spriteBatch.Draw(mirino, new Vector2(InputGame.InputManagerGame.inputState.mouseX - mirino.Width * 0.5f, InputGame.InputManagerGame.inputState.mouseY - mirino.Height * 0.5f), Color.White);
            #endif
            spriteBatch.End();
            
            base.Draw(gameTime);
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

        }
    }
}
